/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.imp;

import dao.ArticuloDao;
import hibernate.HibernateUtil;
import java.util.ArrayList;
import modelo.dominio.Articulo;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Pablo
 */
public class ArticuloDaoImp implements ArticuloDao {

    @Override
    public void insert(Articulo articulo) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(articulo);
        session.getTransaction().commit();
        session.close();                
    }

    @Override
    public void delete(Articulo articulo) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(articulo);
        session.getTransaction().commit();
        session.close();
        
    }

    @Override
    public void update(Articulo articulo) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(articulo);
        session.getTransaction().commit();
        session.close();
       
    }

    @Override
    public Articulo getArticulo(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Articulo.class);
        criteria.add(Restrictions.eq("id", id));
        Articulo articulo = (Articulo) criteria.list().get(0);
        session.close();
        return articulo;
    }

    @Override
    public ArrayList<Articulo> getAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Articulo.class);
        criteria.addOrder(Order.asc("id"));
        ArrayList<Articulo> list = (ArrayList<Articulo>) criteria.list();
        session.close();
        return list;
        
    }
    @Override
      public Articulo getArticuloByCodigo(String codigo){
      Session session = HibernateUtil.getSessionFactory().openSession();      
      Criteria criteria = session.createCriteria(Articulo.class);
      criteria.add(Restrictions.eq("barCode", codigo));
      return (Articulo) criteria.uniqueResult();
    }
   
    
}
